package com.crape.topic.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.crape.common.exceptions.DAOException;
import com.crape.common.ibatis.SqlMapClientDaoSupportEx;
import com.crape.common.model.Article;
import com.crape.topic.dao.IArticleDAO;

@Service
public class ArticleDAOImpl extends SqlMapClientDaoSupportEx implements IArticleDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> searchArticles(String searchValue, Integer startIndex, Integer pageSize) throws DAOException {
		if (startIndex == null || pageSize == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("searchValue", searchValue);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Article>) this.queryList("searchArticles", params);
	}

	@Override
	public Long countSearchArticles(String searchValue) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("searchValue", searchValue);
		return (Long) this.queryObject("countSearchArticles", params);
	}

	@Override
	public Long getMaxArticleId() throws DAOException {
		return (Long) this.queryObject("getMaxArticleId", null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> listArticles(Article article) throws DAOException {
		if (article == null) {
			return null;
		}
		return (List<Article>) this.queryList("listArticles", article);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getHotArticles() throws DAOException {
		return (List<Article>) this.queryList("getHotArticles", null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getNext4Articles(Long subjectid, Long articleid) throws DAOException {
		if (subjectid == null || articleid == null) {
			return null;
		}
		Map<String, Long> params = new HashMap<String, Long>();
		params.put("subjectid", subjectid);
		params.put("articleid", articleid);
		return (List<Article>) this.queryList("getNext4Articles", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getAuthorOtherArticles(Long accountid, Long articleid, Integer pageSize) throws DAOException {
		if (accountid == null || articleid == null || pageSize == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("articleid", articleid);
		params.put("pageSize", pageSize);
		return (List<Article>) this.queryList("getAuthorOtherArticles", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getSubjectOtherArticles(Long subjectid, Long articleid, Integer pageSize) throws DAOException {
		if (subjectid == null || articleid == null || pageSize == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("subjectid", subjectid);
		params.put("articleid", articleid);
		params.put("pageSize", pageSize);
		return (List<Article>) this.queryList("getSubjectOtherArticles", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getAllArticles(Integer startIndex, Integer pageSize) throws DAOException {
		if (startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Article>) this.queryList("getAllArticles", params);
	}

	@Override
	public Long countAllArticles() throws DAOException {
		return (Long) this.queryObject("countAllArticles", null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getArticlesByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws DAOException {
		if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Article>) this.queryList("getArticlesByAccountid", params);
	}

	@Override
	public Long countArticlesByAccountid(Long accountid) throws DAOException {
		if (accountid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countArticlesByAccountid", accountid);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getCommentArticlesByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws DAOException {
		if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Article>) this.queryList("getCommentArticlesByAccountid", params);
	}

	@Override
	public Long countCommentArticlesByAccountid(Long accountid) throws DAOException {
		if (accountid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countCommentArticlesByAccountid", accountid);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> getArticlesBySubjectid(Long subjectid, Integer startIndex, Integer pageSize) throws DAOException {
		if (subjectid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("subjectid", subjectid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Article>) this.queryList("getArticlesBySubjectid", params);
	}

	@Override
	public Long countArticlesBySubjectid(Long subjectid) throws DAOException {
		if (subjectid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countArticlesBySubjectid", subjectid);
	}

	@Override
	public Long insertArticle(Article article) throws DAOException {
		if (article == null) {
			return null;
		}
		this.insert("insertArticle", article);
		return this.getMaxArticleId();
	}

	@Override
	public void updateArticle(Article article) throws DAOException {
		if (article == null) {
			return;
		}
		this.update("updateArticle", article);
	}

	@Override
	public void deleteArticleById(Long id) throws DAOException {
		if (id == null) {
			return;
		}
		this.delete("deleteArticleById", id);
	}

	@Override
	public Long countArticleCommentsByid(Long articleid) throws DAOException {
		if (articleid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countArticleCommentsByid", articleid);
	}

	@Override
	public Long countArticleRecommendsByid(Long articleid) throws DAOException {
		if (articleid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countArticleRecommendsByid", articleid);
	}

	@Override
	public Long countArticleReplysByid(Long articleid) throws DAOException {
		if (articleid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countArticleReplysByid", articleid);
	}

	@Override
	public Long countArticleVisitorsByid(Long articleid) throws DAOException {
		if (articleid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countArticleVisitorsByid", articleid);
	}

	@Override
	public void updateArticleComments(Long id, Long count) throws DAOException {
		if (id == null || count == null) {
			return;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("count", count);
		this.update("updateArticleComments", params);
	}

	@Override
	public void updateArticleRecommends(Long id, Long count) throws DAOException {
		if (id == null || count == null) {
			return;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("count", count);
		this.update("updateArticleRecommends", params);
	}

	@Override
	public void updateArticleReplys(Long id, Long count) throws DAOException {
		if (id == null || count == null) {
			return;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("count", count);
		this.update("updateArticleReplys", params);
	}

	@Override
	public void updateArticleVisitors(Long id, Long count) throws DAOException {
		if (id == null || count == null) {
			return;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("count", count);
		this.update("updateArticleVisitors", params);
	}

	@Override
	public void addArticleViews(Long id) throws DAOException {
		if (id == null) {
			return;
		}
		this.update("addArticleViews", id);
	}

}
